#ifndef _MODULE_DEFEND
#define _MODULE_DEFEND
#include "page.module.js"
#include "prefs.module.js"
#include "boss.module.js"
#include "console.module.js"
#include "task.lib.js"
#include "ajax.lib.js"
#include "cache.lib.js"
#include "utility.lib.js"
#include "usolib.lib.js"

Modules.push( "Defend" );

var Defend = {
	count: 0,
	running: false,
	chain_previous: '',
	log: function(action) {
		var log = [{
			name: 'Script',
			value: 'Defend'
		},
		{
			name: 'Action',
			value: action
		}];
		Console.log(log);
		Console.status('defend', action);
		return;
	},
	init: function(resume) {
		Defend.count = 0;
		if (resume) {
			if (Fight.running === false) return;
		}
		Defend.running = true;
		Defend.individual = false;
		Defend.log('Initiating Defend Automation');
		Ajax(Page.domain + '/mobwars/', function(xhr) {
			Page.captcha(xhr.responseText);
			if (Page.pausing === true) {
				Page.pause_cb(function() {
					Defend.init(true);
					return;
				});
				return;
			};
			Boss.update(xhr.responseText);
			Defend.chain_previous = Task.chain_check( xhr.responseText ).toSource();
			Defend.check_stats( xhr.responseText );
		});
	},
	refresh: function() {
		if (Defend.running === false) {
			return;
		};
		if (Page.pausing === true) {
			Page.pause_cb(function() {
				Defend.refresh();
				return;
			});
			return;
		};
		if (Defend.count >= Settings.script_repeat && -1 != Settings.script_repeat) {
			Page.toggle('defend');
			return;
		}
		Ajax(Page.domain + '/mobwars/', function(xhr) {
			Page.captcha(xhr.responseText);
			if (Page.pausing === true) {
				Page.pause_cb(function() {
					Defend.refresh();
					return;
				});
				return;
			};
			Boss.update(xhr.responseText);
			Defend.check_stats( xhr.responseText );
		});
	},
	check_stats: function( html ) {
		if (Defend.running === false) {
			return;
		};
		if (Page.pausing === true) {
			Page.pause_cb(function() {
				Defend.refresh();
				return;
			});
			return;
		};
		Defend.log('Checking Boss\'s Stats');
		var updating = false;
		Defend.count++;
		if (Boss.cash >= Settings.bank_limit && Settings.bank && Cache.banking === false) {
			Defend.log('Banking ' + Utility.int_dollars(Boss.cash));
			Task.bank(Defend.refresh);
			updating = true;
		}
		if (Boss.health <= Settings.min_health && Settings.heal && Cache.healing === false) {
			Defend.log('Healing your Boss');
			Task.heal(Defend.refresh);
			updating = true;
		}
		if ( Settings.defend_chain ) {
			var chain = Task.chain_check( html ).toSource();
			if ( chain !== Defend.chain_previous ) {
				Defend.chain_previous = chain;
				Page.pausing = true;
				var timer = new Utility.timer({
					time: Settings.defend_dead_wait * 60,
					message: 'Playing dead for [[time]]...',
					script: 'defend',
					fn: function() {
						Page.resume();
						Ajax( Page.domain + '/mobwars/', function( xhr ) {
							Page.captcha(xhr.responseText);
							if (Page.pausing === true) {
								Page.pause_cb(function() {
									Defend.refresh();
								})
								return;
							}
							Defend.chain_previous = Task.chain_check( xhr.responseText ).toSource();
							Defend.refresh();
						});
					}
				});
				timer.start();
				updating = true;
			}
		}
		if (updating) {
			Defend.list = null;
			return;
		}
		Defend.log('Stat checks complete');
		var timer = new Utility.timer({
			time: Settings.defend_time,
			message: 'Checking stats again in [[time]]...',
			script: 'defend',
			fn: function() {
				Defend.refresh();
				return;
			}
		});
		timer.start();
		return;
	}
};
#endif
